package com.tcgroup.phone.base_core.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.RequiresApi;

import com.tcgroup.phone.base_core.R;


/**
 * 作者: wangfei
 * 创建时期: 2020/4/15.
 * 包名: com.tcgroup.phone.base_core.widget
 * 类名: OSAndroidMvvm
 */
@SuppressLint("AppCompatCustomView")
public class TextImageButton extends RelativeLayout {

    private String content;
    private float textSize;
    private @IdRes int textColor;

    private Drawable icon;
    private @IdRes int iconVisibility;//0：显示 1:不显示

    private @IdRes int subBackground;

    private RelativeLayout mSublayout;
    private ImageView mIvIcon;
    private TextView mTvTitle;
    private Boolean flag = false;//根据该值来判断是否被选中false:没有被选中

    public OnItemClickListener OnItemClickListener;

    public TextImageButton(Context context) {
        super(context);
    }

    public TextImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.TextImageButton);
        content = array.getString(R.styleable.TextImageButton_title);
        textSize = array.getDimension(R.styleable.TextImageButton_title_size, 10);
        textColor = array.getColor(R.styleable.TextImageButton_title_color,context.getResources().getColor(R.color.transparent160_gray));
        subBackground = array.getResourceId(R.styleable.TextImageButton_background,context.getResources().getColor(R.color.transparent100_white));
        icon = array.getDrawable(R.styleable.TextImageButton_icon);
        iconVisibility = array.getInteger(R.styleable.TextImageButton_icon_visibility,0);
        array.recycle();
        initView();
    }

    public TextImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TextImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @SuppressLint("ResourceType")
    public void initView(){
        mSublayout = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.core_widget_text_image_button,this,true);
        mIvIcon = mSublayout.findViewById(R.id.iv_icon);
        mTvTitle = mSublayout.findViewById(R.id.tv_title);
        setIcon(icon);
        mTvTitle.setTextColor(textColor);
        mTvTitle.setTextSize(textSize);
        mTvTitle.setText(content == null ? "":content);
        mSublayout.setBackgroundResource(subBackground);
        mSublayout.setOnClickListener(v -> {
            if (null != OnItemClickListener){
                flag = !flag;
                OnItemClickListener.setOnItemClick(getId(),flag);
            }
        });
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public Drawable getIcon() {
        return icon;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
        if (iconVisibility == 0){
            mIvIcon.setImageDrawable(this.icon);
        }else {
            mIvIcon.setVisibility(View.GONE);
        }
    }

    public ImageView getmIvIcon() {
        return mIvIcon;
    }


    public TextView getmTvTitle() {
        return mTvTitle;
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.OnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener{
        void setOnItemClick(int id,boolean isChoose);
    }
}
